home *** CD-ROM | disk | FTP | other *** search
/ Aminet 30 / Aminet 30 (1999)(Schatztruhe)[!][Apr 1999].iso / Aminet / dev / cross / GBDK-2.0.lha / GBDK / examples / comm.c < prev    next >
C/C++ Source or Header  |  1998-10-01  |  2KB  |  94 lines

  1. #include <gb.h>
  2. #include <stdio.h>
  3.  
  4. char *str = "Hello World!";
  5. char buffer[32];
  6.  
  7. void main()
  8. {
  9.   UBYTE i, j, n = 0;
  10.   char *s;
  11.  
  12.   puts("Byte");
  13.   puts("  A      : Send");
  14.   puts("  B      : Receive");
  15.   puts("String");
  16.   puts("  START  : Send");
  17.   puts("  SELECT : Receive");
  18.  
  19.   while(1) {
  20.     i = waitpad(J_A | J_B | J_START | J_SELECT);
  21.     waitpadup();
  22.  
  23.     if(i == J_A) {
  24.       /* Send 1 byte */
  25.       printf("Sending b... ");
  26.       _io_out = n++;
  27.       send_byte();
  28.       /* Wait for IO completion... */
  29.       while(_io_status == IO_SENDING && joypad() == 0)
  30.     ;
  31.       if(_io_status == IO_IDLE)
  32.     printf("OK\n");
  33.       else
  34.     printf("#%d\n", _io_status);
  35.     } else if(i == J_B) {
  36.       /* Receive 1 byte */
  37.       printf("Receiving b... ");
  38.       receive_byte();
  39.       /* Wait for IO completion... */
  40.       while(_io_status == IO_RECEIVING && joypad() == 0)
  41.     ;
  42.       if(_io_status == IO_IDLE)
  43.     printf("OK\n%d\n", _io_in);
  44.       else
  45.     printf("#%d\n", _io_status);
  46.     } else if(i == J_START) {
  47.       /* Send a string */
  48.       printf("Sending s... ");
  49.       s = str;
  50.       while(1) {
  51.     _io_out = *s;
  52.     do {
  53.       send_byte();
  54.       /* Wait for IO completion... */
  55.       while(_io_status == IO_SENDING && joypad() == 0)
  56.         ;
  57.     } while(_io_status != IO_IDLE && joypad() == 0);
  58.     if(_io_status != IO_IDLE) {
  59.       printf("#%d\n", _io_status);
  60.       break;
  61.     }
  62.     if(*s == 0)
  63.       break;
  64.     s++;
  65.       }
  66.       if(_io_status == IO_IDLE)
  67.     printf("OK\n");
  68.     } else if(i == J_SELECT) {
  69.       /* Receive a string */
  70.       printf("Receiving s... ");
  71.       s = buffer;
  72.       while(1) {
  73.     receive_byte();
  74.     /* Wait for IO completion... */
  75.     while(_io_status == IO_RECEIVING && joypad() == 0)
  76.       ;
  77.     if(_io_status != IO_IDLE) {
  78.       printf("#%d\n", _io_status);
  79.       break;
  80.     }
  81.     putchar(_io_in);
  82.     *s = _io_in;
  83.     if(*s == 0)
  84.       break;
  85.     s++;
  86.       }
  87.       if(_io_status == IO_IDLE)
  88.     printf("OK\n%s\n", buffer);
  89.     }
  90.     /* In case of user cancellation */
  91.     waitpadup();
  92.   }
  93. }
  94.